import { inject, injectable } from 'inversify'
import { NotebookModel, NotebooksService } from '../service/NotebooksService'

// @injectable表明将当前类注入到ioc容器中
@injectable()
export class NotebooksController {
  // 通过构造方法，可以实现，自动将ioc容器中实例，注入到当前实例对象中. 如: service: NotebooksService, 后续的类方法就可直接使用
  constructor(@inject(NotebooksService) public service: NotebooksService) {}

  async create(name: string) {
    return await this.service.create({
      name
    })
  }

  async delete(id: number) {
    const row = await this.service.get(id)
    if (row) {
      await this.service.delete(id)
    }
  }

  async update(data: { id: number; name: string }) {
    return await this.service.update(data)
  }

  async getAll(): Promise<NotebookModel[]> {
    return await this.service.getAll()
  }
}
